package com.kuqi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.kuqi.R;
import com.kuqi.entitiy.AreaItem;

import java.util.List;

public class OtherPageGridAdapter extends BaseAdapter{

	private Context mContext;
	private List<AreaItem> mList;

	public OtherPageGridAdapter(Context mContext, List<AreaItem> mList){
		this.mContext = mContext;
		this.mList = mList;	
	}
	

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public AreaItem getItem(int position) {
		return mList == null ? null : mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// 使用view holder来管理控件
		ViewHolder viewholder;
		AreaItem areaItem = getItem(position);
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.area_item, null);
			viewholder = new OtherPageGridAdapter.ViewHolder();
			viewholder.areaImageView = convertView.findViewById(R.id.areaImageView);
			viewholder.areaTextView = convertView.findViewById(R.id.areaTextView);
			convertView.setTag(viewholder);
		}else{
			viewholder = (OtherPageGridAdapter.ViewHolder) convertView.getTag();
		}

		// 设置属性及事件
		viewholder.areaImageView.setImageResource(areaItem.getImg());
		viewholder.areaTextView.setText(areaItem.getText());
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(mContext,"模块功能还在施工ing...",Toast.LENGTH_LONG).show();
			}
		});
		return convertView;
	}


	// view holder
	private static class ViewHolder{
		private ImageView areaImageView;
		private TextView areaTextView;
	}

}
